//
//  CXYSongsMenuTableViewCell.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/14.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "CXYSongsMenuTableViewCell.h"
#import "CustomerNetworking.h"
#import "SongsMenuButtonModel.h"
#import "CXYSongsMenuDataCollectionViewCell.h"
#import "CXYSongsmMenuCollectionReusableView.h"

#define kWidth self.frame.size.width
#define kHeight self.frame.size.height


@interface CXYSongsMenuTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSUserDefaults *user;
@property (nonatomic, retain) UICollectionView *collectionView;
@end

@implementation CXYSongsMenuTableViewCell

- (void)dealloc
{
    [_buttonArray release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.buttonArray = [NSMutableArray array];
        self.user = [NSUserDefaults standardUserDefaults];
        [self customer];
        [self createCollectionView];
    }
    return self;
}

- (void)customer
{
    [CustomerNetworking  customerConnectWithURL:@"http://api.dongting.com/song/tag/songlist?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679246042943&longtitude=121.5286685343515" andParameters:nil andBlock:^(id response) {
        for (NSDictionary *dic in [response objectForKey:@"data"]) {
            SongsMenuButtonModel *songsMenuButtonModel = [SongsMenuButtonModel songsMenuButtonModelWithDictionary:dic];
            [self.buttonArray addObject:songsMenuButtonModel];
        }
        [self.collectionView reloadData];
    }];
}


- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64 - 30) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[CXYSongsMenuDataCollectionViewCell class] forCellWithReuseIdentifier:@"make"];
    //头视图
    [self.collectionView registerClass:[CXYSongsmMenuCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kkk"];
    [self.contentView addSubview:_collectionView];
    [_collectionView release];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(_collectionView.frame.size.width, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        CXYSongsmMenuCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kkk" forIndexPath:indexPath];
        reusableView.string = [[self.buttonArray objectAtIndex:indexPath.section] tagTitle];
        reusableView.titleLabel.font = [UIFont systemFontOfSize:17];
        return reusableView;
    }
    return nil;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _buttonArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[[_buttonArray objectAtIndex:section] childrenArray] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXYSongsMenuDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"make" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.titleString = [[[[self.buttonArray objectAtIndex:indexPath.section] childrenArray] objectAtIndex:indexPath.row] tagTitle];
    NSString *string = [_user objectForKey:@"buttonText"];
    if ([cell.titleString isEqual:string]) {
        cell.titleLabel.textColor = [UIColor greenColor];
    }else{
        cell.titleLabel.textColor = [UIColor whiteColor];
    }
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.frame.size.width - 50) / 4, self.frame.size.height / 10);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate popSongMenuController:[[[[self.buttonArray objectAtIndex:indexPath.section] childrenArray] objectAtIndex:indexPath.row] tagTitle]];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
